package com.ctgu.controller;

import com.ctgu.service.CourseService;
import com.ctgu.service.HomeworkDetailService;
import com.ctgu.service.HomeworkService;
import com.ctgu.service.ScoreService;
import com.ctgu.vo.Course;
import com.ctgu.vo.Homework;
import com.ctgu.vo.Score;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Resource
    CourseService courseService;

    @Resource
    ScoreService scoreService;

    @Resource
    HomeworkService homeworkService;

    @Resource
    HomeworkDetailService homeworkDetailService;

    //成绩查询
    @RequestMapping("/grade")
    public String grade(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        List<Score> list =  scoreService.selectScoreByStudentId(userId);
        model.addAttribute("list",list);
        return "student/studentGrade";
    }

    //选课
    @RequestMapping("/selectCourse")
    public String selectCourse(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        List<Course> list =  courseService.getAllCourse();
        model.addAttribute("list",list);
        for (Course c:list){
            System.out.println(c.getCourseId()+" "+c.getCourseName()+" "+c.getTeacher().getTeacherName());
        }
        return "student/studentClass";
    }

    //查看已选课程
    @RequestMapping("/mineCourse")
    public String mineCourse(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        List<Course> list =  courseService.getCourseByUserId(userId);
        model.addAttribute("list",list);

        return "student/studentMineClass";

    }

    //学生所有作业
    @RequestMapping("/allHomework")
    public String getAllHomework(HttpServletRequest request, HttpServletResponse response, Model model){
        int userId = (int) request.getSession().getAttribute("userId");
        List<Homework> list = homeworkService.selectCourseHomeworkTeacherByStudentId(userId);
        model.addAttribute("list",list);
        return "student/studentHomework";
    }

    //学生选课
    @RequestMapping("/addScore")
    @ResponseBody
    public String addScore(HttpServletRequest request, HttpServletResponse response,String courseId){
        int userId = (int) request.getSession().getAttribute("userId");
        Score score = scoreService.selectBycourseIdstudentId(userId,courseId);
        if(score == null){//如果成绩表不存在已选信息
            scoreService.insertScore(userId,courseId);
            return "success";
        }else {
            return "fail";
        }
    }

    //学生退课
    @RequestMapping("/exitCourse")
    public String exitCourse(HttpServletRequest request, HttpServletResponse response,String courseId){
        int userId = (int) request.getSession().getAttribute("userId");
        scoreService.deleteCourse(userId, courseId);
        return "redirect:/student/mineCourse";
    }

    //获取作业标题
    @RequestMapping("/homeworkDetail")
    public String homeworkDetail(HttpServletRequest request, HttpServletResponse response,String homeworkTitle,String homeworkId,String homeworkDetail){
        request.getSession().setAttribute("title",homeworkTitle);
        request.getSession().setAttribute("num",homeworkId);
        request.getSession().setAttribute("detail",homeworkDetail);
        return "student/studentHomeworkSubmit";
    }

    //提交作业
    @RequestMapping("/homeworkSubmit")
    public String homeworkSubmit(HttpServletRequest request, HttpServletResponse response){
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String homework = request.getParameter("desc");
        if(request.getParameter("homeworkId")!=null&&request.getParameter("userId")!=null){
            int homeworkId = Integer.parseInt(request.getParameter("homeworkId"));
            int userId = Integer.parseInt(request.getParameter("userId"));
            homeworkDetailService.homeworkSubmit(homeworkId,userId,homework);
        }
        return "redirect:/student/allHomework";
    }
}
